package softdone;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

import softdata.Pla_data;
import softdata.Pos_data;
import softdata.Pla_item;

public class Pos_done{
	public Pos_data Postdata=new Pos_data();
	public Collection Postplates;
	public int Num_Pos_Plates;
	public int Num_Plates_Click=0;
	public String Nam_Plates;
	public int Sum_Posts=0;		// 主题帖总数
	

public void Pos_Read(int id){
	/* 传入参数： 帖子id
	 * 函数功能： 帖子读取
	 */
	String sql="{call posts_read(?)}";
	try{
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1,id);
		ResultSet rs=cs.executeQuery();
		if(rs.next())
		{
			Postdata.ID_Plates=rs.getInt(1);
			Postdata.ID_User=rs.getInt(2);
			Postdata.Num_Reply=rs.getInt(3);
			Postdata.Title=rs.getString(4);
			Postdata.Content=rs.getString(5);
			Postdata.Tim_Post=rs.getString(6);
			Postdata.Num_Clicks=rs.getInt(7);
			Postdata.Name_User=rs.getString(8);
			Postdata.PhoSrc=rs.getString(9);
			Postdata.Integral=rs.getInt(10);
			Postdata.Name_Plates=rs.getString(11);
		}
		rs.close();
		dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}

public void Pos_Pla_Read(){
	/*
	 *  函数功能： 读取具体板块帖子的帖子 列表
	 */

	String sql="{call post_plates_read()}";
	try{
		Postplates=new ArrayList();
		Num_Pos_Plates=0;
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		ResultSet rs=cs.executeQuery();
		while(rs.next())
		{
			Num_Pos_Plates++;
			Pla_item item=new Pla_item();		// 板块信息
			item.ID=rs.getInt(1);
			item.Nam_Plate=rs.getString(2);
			Postplates.add(item);
		}
		rs.close();
		dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}
public void Pla_Read(int id){
	/* 	传入参数： 板块id
	 *  函数功能： 读取具体板块信息
	 */
	String sql1="{call posts_plates_getname(?)}";
	String sql2="{call posts_plates_create()}";
	String sql3="{call posts_plates_read(?)}";
	try{
		Postplates=new ArrayList();
		Num_Pos_Plates=0;
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql1);
		cs.setInt(1,id);
		ResultSet rs=cs.executeQuery();
		if(rs.next()) 
		{	
			Nam_Plates=rs.getString(1);
			Num_Plates_Click=rs.getInt(2);
		}
		cs=dataconn.CSt_SQL(sql2);
		cs.execute();
		cs=dataconn.CSt_SQL(sql3);
		cs.setInt(1,id);
		rs=cs.executeQuery();
		while(rs.next())
		{
			Num_Pos_Plates++;
			Pla_data platesdata=new Pla_data();
			platesdata.ID_Post=rs.getInt(1);
			platesdata.ID_User=rs.getInt(2);
			platesdata.Num_Reply=rs.getInt(3);
			platesdata.Title=rs.getString(4);
			platesdata.Tim_Post=rs.getString(5);
			platesdata.Num_Clicks=rs.getInt(6);
			platesdata.ID_Use_Last=rs.getInt(7);
			platesdata.Tim_Last=rs.getString(8);
 			platesdata.Nam_User=rs.getString(9);
 			platesdata.Nam_Use_Last=rs.getString(10);
			Postplates.add(platesdata);
			platesdata=null;
		}
		dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}
public void Pos_delete(int id){
	/*	
	 * 函数功能： 帖子删除
	 */
	
}
public void Pos_edit(int id,int id_p,String title,String content){
	/* 
	 *  函数功能： 帖子修改
	 */
	String sql="{call posts_change(?,?,?,?)}";
	try{
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1,id);
		cs.setInt(2,id_p);
		cs.setString(3,title);
		cs.setString(4,content);
		cs.execute();
	    dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}
public void Pos_insert(int id_p,int id_u,String title,String content){
	/* 
	 * 函数功能： 存储帖子信息
	 */
	String sql="{call posts_insert(?,?,?,?,?)}";
	try{
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1,id_p);
		cs.setInt(2,id_u);
		cs.setString(3,title);
		cs.setString(4,content);
		cs.registerOutParameter(5,java.sql.Types.INTEGER);
		cs.execute();
		Postdata.ID_Post=cs.getInt(5);
	    dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}
public String GetNam_Plates(){
	return Nam_Plates;
}
public void PostListRead(int page){
	String sql="{call post_list_read(?)}";
	try{
		Postplates=new ArrayList();
		Num_Pos_Plates=0;
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1,page);
		ResultSet rs=cs.executeQuery();
		while(rs.next())
		{
			Pla_data platesdata=new Pla_data();
			platesdata.ID_Post=rs.getInt(1);
			platesdata.Title=rs.getString(2);
			platesdata.ID_User=rs.getInt(3);
			platesdata.Tim_Post=rs.getString(4);
			platesdata.Num_Reply=rs.getInt(5);
 			platesdata.Nam_User=rs.getString(6);
			Postplates.add(platesdata);
			Num_Pos_Plates++;
		}
		dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}

public int Sum_Post(){
	/* 
	 *  函数功能： 读取帖子总数
	 */
	int Sum=0;
	String sql="{call post_sum(?)}";
	try{
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.registerOutParameter(1,java.sql.Types.INTEGER);
		cs.execute();
		Sum=cs.getInt(1);
	    dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
	return Sum;
}

public void Post_Delete(int id){
	/*
	 * 函数功能： 删除 帖子及帖子的回复
	 */
	String sql="{call posts_delete(?)}";
	try{
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1,id);
		cs.execute();
	    dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}
public void Post_Search_List(int page,String key){
	String sql="{call post_rea_all(?,?,?)}";
	try{
		Postplates=new ArrayList();
		Num_Pos_Plates=0;
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1,page);
		cs.setString(2,key);
		cs.registerOutParameter(3,java.sql.Types.INTEGER);
		cs.execute();
		Sum_Posts=cs.getInt(3);
		ResultSet rs=cs.executeQuery();
		while(rs.next())
		{
			Pla_data platesdata=new Pla_data();
			platesdata.ID_Post=rs.getInt(1);
			platesdata.ID_User=rs.getInt(2);
			platesdata.Title=rs.getString(3);
 			platesdata.Nam_User=rs.getString(4);
			Postplates.add(platesdata);
			Num_Pos_Plates++;
		}
		rs.close();
		dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
}

public int  Num_User_Posts(int id){
	/*
	 * 读取用户帖子数
	 */
	int Sum=0;
	String sql="{call post_sum_user(?,?)}";
	try{
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1, id);
		cs.registerOutParameter(2,java.sql.Types.INTEGER);
		cs.execute();
		Sum=cs.getInt(2);
	    dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
	return Sum;
}

public void PostList_User(int page,int user){
	/*
	 * 个人中心读取帖子列表 ,按用户名，页数
	 * Num_Pos_Plates 表示当前页帖子数
	 */
	String sql="{call post_user_list20(?,?)}";
	try{
		Postplates=new ArrayList();
		Num_Pos_Plates=0;
		Conn dataconn=new Conn();
		dataconn.Create();
		CallableStatement cs=dataconn.CSt_SQL(sql);
		cs.setInt(1,page);
		cs.setInt(2,user);
		ResultSet rs=cs.executeQuery();
		while(rs.next())
		{
			Pos_data platesdata=new Pos_data();
			platesdata.ID_Post=rs.getInt(1);
			platesdata.Title=rs.getString(2);
			platesdata.ID_User=rs.getInt(3);
			platesdata.Tim_ReplyLast=rs.getString(4);
			platesdata.Num_Reply=rs.getInt(5);
 			platesdata.Num_Clicks=rs.getInt(6);
 			platesdata.Name_User=rs.getString(6);
			Postplates.add(platesdata);
			Num_Pos_Plates++;
		}
		dataconn.CloseCC();
	}catch(Exception e){
		e.printStackTrace();
	}
	
}
}